<?php 
if(KT!='KhiThe') exit;
class products_model extends khithe_mysql{

	public function setCheckedProducts($ID,$Val)
	{
		$ID = parent::checkStrMySQL($ID);
		$update = "UPDATE ".products." SET Status='".$Val."'  WHERE ID = '".$ID."'";
		$update_qr = parent::UpdateInsertMySQL($update);
		parent::CloseConnection();
		return $update_qr;
	}
	
	public function delCheckedProducts($ID)
	{
		$ID = parent::checkStrMySQL($ID);
		$del = "DELETE FROM ".products." WHERE ID='".$ID."' ";
		$del_qr = parent::DeleteMySQL($del);
		parent::CloseConnection();
		return $del_qr;
	}
	
	public function mdcheckSeoKey($SeoKey)
	{
		$SeoKey = parent::checkStrMySQL($SeoKey);
		$select = "SELECT * FROM ".products." WHERE SeoKey = '".$SeoKey."'";
		$resultSql= parent::SelectMySQL($select);
		$total = mysql_num_rows($resultSql);
		parent::CloseConnection();
		return $total;
	}
	public function updateInsertProduct($_POST)
	{
		$ID = parent::checkStrMySQL($_POST['ID']);
		$Title = parent::checkStrMySQL($_POST['txt_titleProduct']);
		$CategoryID = parent::checkStrMySQL($_POST['cb_select']);
		$ManufacturerID = parent::checkStrMySQL($_POST['cb_manufacturer']);
		$Picture = parent::checkStrMySQL($_POST['Picture']);
		$Picture_1 = parent::checkStrMySQL($_POST['Picture_1']);
		$Picture_2 = parent::checkStrMySQL($_POST['Picture_2']);
		$Picture_3 = parent::checkStrMySQL($_POST['Picture_3']);
		$Picture_4 = parent::checkStrMySQL($_POST['Picture_4']);
		$Warranty = parent::checkStrMySQL($_POST['txt_warranty']);
		$Price = parent::checkStrMySQL($_POST['txt_price']);
		$PriceSpecial = parent::checkStrMySQL($_POST['txt_priceSpecial']);
		
		$Promotion = parent::checkStrMySQL($_POST['txt_promotion']);
		$From = parent::checkStrMySQL($_POST['txt_From']);
		$To = parent::checkStrMySQL($_POST['txt_To']);
		$From=($From==''?'':strtotime($From));
		$To=($To==''?'':strtotime($To));
		$Special = parent::checkStrMySQL($_POST['txt_Special']=='true'?'true':'false');
		$Details = parent::checkStrMySQL($_POST['txt_textarea']);
		
		$Pic = $Picture!='InvalidFile'?" Picture='".$Picture."', ":'';
		$Pic_1 = $Picture_1!='InvalidFile'?" Picture_1='".$Picture_1."', ":'';
		$Pic_2 = $Picture_2!='InvalidFile'?" Picture_2='".$Picture_2."', ":'';
		$Pic_3 = $Picture_3!='InvalidFile'?" Picture_3='".$Picture_3."', ":'';
		$Pic_4 = $Picture_4!='InvalidFile'?" Picture_4='".$Picture_4."', ":'';
		
		/*SEO*/
        $SeoKey = parent::checkStrMySQL($_POST['txt_seo_key']);

		$TitlePage = parent::checkStrMySQL($_POST['txt_title_page']);
		$KeyWords = parent::checkStrMySQL($_POST['txt_keywords']);
		$Description = parent::checkStrMySQL($_POST['txt_description']);
		$SeoH1 = parent::checkStrMySQL($_POST['txt_seo_h1']);
		$SeoH2 = parent::checkStrMySQL($_POST['txt_seo_h2']);
		$SeoP = parent::checkStrMySQL($_POST['txt_seo_p']);
		
		if($_POST['acProduct']=='edit'){
			$query = "UPDATE ".products." SET Title='".$Title."', CategoryID='".trim($CategoryID)."', ManufacturerID='".trim($ManufacturerID)."',".$Pic." ".$Pic_1." ".$Pic_2." ".$Pic_3." ".$Pic_4." Warranty= '".$Warranty."',Price= '".$Price."',PriceSpecial= '".$PriceSpecial."',Promotion='".$Promotion."',`From`= '".$From."',`To`= '".$To."',Special= '".$Special."',Details= '".$Details."',SeoKey='".$SeoKey."',TitlePage = '".trim($TitlePage)."',Description = '".$Description."',KeyWords = '".$KeyWords."',SeoH1 = '".$SeoH1."',SeoH2 = '".$SeoH2."',SeoP = '".$SeoP."' WHERE ID = '".trim($ID)."'";
		
		}else if($_POST['acProduct']=='add' && $Picture!='InvalidFile'){
			$query = "INSERT INTO ".products."( Title, CategoryID, ManufacturerID, Picture, Picture_1, Picture_2, Picture_3, Picture_4, Warranty, Price, PriceSpecial, Promotion, `From`, `To`, Special, Details,SeoKey, TitlePage, Description, KeyWords, SeoH1, SeoH2, SeoP, `Status`, PostDate,`Action`) VALUES ('".$Title."', '".trim($CategoryID)."', '".trim($ManufacturerID)."', '".$Picture."','".$Picture_1."','".$Picture_2."','".$Picture_3."','".$Picture_4."', '".$Warranty."', '".$Price."', '".$PriceSpecial."', '".$Promotion."', '".$From."', '".$To."', '".$Special."', '".$Details."','".$SeoKey."','".trim($TitlePage)."','".$Description."','".$KeyWords."','".$SeoH1."','".$SeoH2."','".$SeoP."', 'true', '".time()."','view' )";
		}
		
		$query = parent::UpdateInsertMySQL($query);
		parent::CloseConnection();
		return $query;
		
	}
	
	public function getMenuProducts($ParentID)
	{
		$ParentID = parent::checkStrMySQL($ParentID);
		$select = "SELECT * FROM ".categorys." WHERE comp='products' AND ParentID = '".$ParentID."' ORDER BY `Order` ASC";
		parent::CloseConnection();
		return parent::SelectMySQL($select);
	}
	
	public function getManufacturer()
	{
		$select = "SELECT * FROM ".manufacturers." ORDER BY `Order` ASC";
		return parent::SelectMySQL($select);
	}
	
	public function getSqlProducts($ParentID,$page,$limit)
	{
		$ParentID = parent::checkStrMySQL($ParentID);
		
		$strPID = " CategoryID='".$ParentID."'";
		$resultMenu = $this->getMenuProducts($ParentID);
		while( $rowMenu = mysql_fetch_assoc($resultMenu) )
		{
			$strPID = $strPID." OR CategoryID='".$rowMenu['ID']."'";		
		}
		
		$str = $ParentID=="All"?"":" WHERE $strPID ";
		if($limit!=''){
			$page=($page==''?1:$page);
			$start = ($page - 1)* $limit; 
			$strlimit = "LIMIT $start, $limit";
		}
		else{
			$strlimit ='';
		}
		$select = "SELECT * FROM ".products." ".$str." ORDER BY ID DESC ".$strlimit." ";
		parent::CloseConnection();
		return parent::SelectMySQL($select);
	}
	
	public function getMenu($ID)
	{
		$ID = parent::checkStrMySQL($ID);
		$select = "SELECT * FROM ".categorys." WHERE comp='products' AND ID = '".$ID."'";
		$query = parent::SelectMySQL($select);
		parent::CloseConnection();
		return $query;
	}
	
	public function selectProduct($ID)
	{
		$ID = parent::checkStrMySQL($ID);
		$select = "SELECT * FROM ".products." WHERE ID = '".$ID."'";
		$query = parent::SelectMySQL($select);
		$row = mysql_fetch_assoc($query);
		parent::CloseConnection();
		
		return $row;
	}
	
	public function GetId()
	{
		$ID = parent::checkStrMySQL($ID);
		$select = "SELECT * FROM ".products." ORDER BY ID DESC";
		$query = parent::SelectMySQL($select);
		$row = mysql_fetch_assoc($query);
		parent::CloseConnection();
		
		return $row['ID'];
	}
}
?>